<template>
  <div id="zhfxyp" style="padding-top:10px">
    <el-tabs v-model="showtype" @tab-click="handleClick">
      <el-tab-pane :label="unitname+' 综合分析研判信息'" name="unit">
        <div id="unit">
          <div style="float:right">
            <el-tooltip class="item" effect="dark" :content="'添加 '+unitname+' 综合分析研判信息'" placement="top">
              <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addUnitAnalysis" v-if="!readOnly">添加</el-button>
            </el-tooltip>
          </div>
          <div v-if="JSON.stringify(unitAnalysisList)!=='[]'">
            <div style="padding-left:10px">年度：
              <el-select v-model="unitAnalysisId" @change="timestamp = new Date().getTime()" placeholder="请选择" style="width:120px">
                <el-option
                  v-for="(item,index) in unitAnalysisList"
                  :key="index"
                  :label="item.nd"
                  :value="item.id">
                </el-option>
              </el-select>
              <el-tooltip class="item" effect="dark" content="删除该年度综合分析研判" placement="top">
                <el-button type="danger" icon="el-icon-delete" @click="delUnitAnalysis" :loading="delloading" v-if="!readOnly">删除</el-button>
              </el-tooltip>
              <!-- <el-tooltip class="item" effect="dark" content="导出该年度综合分析研判" placement="top">
                <el-button type="success" icon="el-icon-download" @click="exportword">导出</el-button>
              </el-tooltip> -->
            </div>
          </div>
          <div v-else style="line-height:40px;font-size:var(--size14);padding-left:10px">{{loadingText}}</div>
        </div>
        <unitAnalysisEdit :timestamp="timestamp" @refresh="getUnitAnalysisList" :readOnly="readOnly" v-if="unitAnalysisId!==''||unitAnalysisShow" v-bind:id="unitAnalysisId" :idCardNo="personId" :unitname="unitname" :unitcode="unitcode"></unitAnalysisEdit>
      </el-tab-pane>
      <el-tab-pane :label="personname+' 综合分析研判信息'" name="person">
        <div id="person">
          <div style="float:right">
            <el-tooltip class="item" effect="dark" :content="'添加 '+personname+' 综合分析研判信息'" placement="top" v-if="!readOnly">
              <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addPersonAnalysis">添加</el-button>
            </el-tooltip>
          </div>
          <div v-if="JSON.stringify(personAnalysisList)!=='[]'">
            <div style="padding-left:10px">年度：
              <el-select v-model="personAnalysisId" @change="timestamp = new Date().getTime()" placeholder="请选择" style="width:120px">
                <el-option
                  v-for="(item,index) in personAnalysisList"
                  :key="index"
                  :label="item.nd"
                  :value="item.id">
                </el-option>
              </el-select>
              <el-tooltip class="item" effect="dark" content="删除该年度综合分析研判" placement="top" v-if="!readOnly">
                <el-button type="danger" icon="el-icon-delete" @click="delPersonAnalysis" :loading="delloading">删除</el-button>
              </el-tooltip>
              <!-- <el-tooltip class="item" effect="dark" content="导出该年度综合分析研判" placement="top">
                <el-button type="success" icon="el-icon-download" @click="exportword">导出</el-button>
              </el-tooltip> -->
            </div>
          </div>
          <div v-else style="line-height:40px;font-size:var(--size14);padding-left:10px">{{loadingText1}}</div>
        </div>
        <personAnalysisEdit :timestamp="timestamp" @refresh="getPersonAnalysisList" :readOnly="readOnly" v-if="personAnalysisId||personAnalysisShow" v-bind:id="personAnalysisId" :idCardNo="personId" :unitname="unitname" :unitcode="unitcode"></personAnalysisEdit>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import personAnalysisEdit from '@/analysis/Edit.vue'
import unitAnalysisEdit from '@/analysis/unitEdit.vue'
export default {
  components: {
    personAnalysisEdit,
    unitAnalysisEdit
  },
  watch: {
    personId: {
      handler (val) {
        this.personId = val
        this.getPersonAnalysisList(val)
      },
      // 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
      immediate: true
    },
    personname: {
      handler (val) {
        this.personname = val
      },
      // 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
      immediate: true
    },
    unitname: {
      handler (val) {
        this.unitname = val
        this.getUnitAnalysisList(val)
      },
      // 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
      immediate: true
    }
  },
  props: {
    personId: '',
    personname: '',
    unitname: '',
    unitcode: ''
  },
  data () {
    return {
      loadingText: '正在加载中...',
      loadingText1: '正在加载中...',
      showtype: 'unit',
      readOnly: true,
      delloading: false,
      timestamp: '',
      unitAnalysisList: [],
      unitAnalysisId: '',
      unitAnalysisShow: false,
      personAnalysisList: [],
      personAnalysisId: '',
      personAnalysisShow: false
    }
  },
  methods: {
    // exportword () {
    //   window.open(this.$http.defaults.baseURL + 'common/fenxiyanpan/wordfile/' + this.personId + '/' + this.zhfxypYear)
    // },
    handleClick (tab, event) {
      console.log(tab, event)
    },
    getAnalysisRoles () {
      this.$http
        .get(this.$http.defaults.analysisAPI + `api/synthesis/security/getCurrentAuth`)
        .then(response => {
          if (response.data.success) {
            this.readOnly = !(response.data.result === 1)
            // this.readOnly = false
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    getPersonAnalysisList (idCard, nd) {
      console.log('测试返回id')
      console.log(nd)
      this.personAnalysisList = []
      let para = {idCard: idCard}
      this.$http
        .post(this.$http.defaults.analysisAPI + `api/busPerson/queryByIdCard`, para)
        .then(response => {
          if (response.data.success) {
            this.loadingText1 = '无个人综合分析研判信息'
            this.personAnalysisList = response.data.result
            if (this.personAnalysisList && this.personAnalysisList.length > 0) {
              if (nd) {
                this.personAnalysisId = this.personAnalysisList.find(item => item.nd === nd).id
              } else {
                this.personAnalysisId = this.personAnalysisList[this.personAnalysisList.length - 1].id
              }
            } else {
              this.personAnalysisId = ''
            }
            this.personAnalysisShow = false
            this.timestamp = new Date().getTime()
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    getUnitAnalysisList (unitname, nd) {
      console.log('测试返回单位名')
      console.log(unitname)
      console.log('测试返回nd')
      console.log(nd)
      this.unitAnalysisList = []
      let para = {name: unitname}
      this.$http
        .post(this.$http.defaults.analysisAPI + `api/busUnit/queryByName`, para)
        .then(response => {
          if (response.data.success) {
            this.loadingText = '无单位综合分析研判信息'
            this.unitAnalysisList = response.data.result
            if (this.unitAnalysisList && this.unitAnalysisList.length > 0) {
              if (nd) {
                this.unitAnalysisId = this.unitAnalysisList.find(item => item.nd === nd).id
              } else {
                this.unitAnalysisId = this.unitAnalysisList[this.unitAnalysisList.length - 1].id
              }
            } else {
              this.unitAnalysisId = ''
            }
            this.unitAnalysisShow = false
            this.timestamp = new Date().getTime()
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    addPersonAnalysis () {
      this.personAnalysisId = ''
      this.personAnalysisShow = true
      this.timestamp = new Date().getTime()
    },
    addUnitAnalysis () {
      this.unitAnalysisId = ''
      this.unitAnalysisShow = true
      this.timestamp = new Date().getTime()
    },
    delPersonAnalysis () {
      if (this.personAnalysisId && this.personAnalysisId !== '') {
        this.$confirm('确定要删除该年度个人综合分析研判信息吗?', '提示', {
          type: 'success'
        }).then(() => {
          let para = {
            id: this.personAnalysisId
          }
          this.delloading = true
          this.$http
            .post(this.$http.defaults.analysisAPI + `api/busPerson/remove`, para)
            .then(response => {
              if (response.data.success === true) {
                this.getPersonAnalysisList(this.personId)
                this.personAnalysisList = ''
                this.personAnalysisId = ''
                this.timestamp = new Date().getTime()
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
              } else {
                this.$message({
                  message: response.data.message,
                  type: 'error'
                })
              }
              this.delloading = false
            })
            .catch(error => {
              this.$message({
                message: '删除失败:' + error,
                type: 'error'
              })
              this.delloading = false
            })
        })
      } else {
        this.$message({
          message: '请选择要删除的年度',
          type: 'error'
        })
      }
    },
    delUnitAnalysis () {
      if (this.unitAnalysisId && this.unitAnalysisId !== '') {
        this.$confirm('确定要删除该年度班子综合分析研判信息吗?', '提示', {
          type: 'success'
        }).then(() => {
          let para = {
            id: this.unitAnalysisId
          }
          this.delloading = true
          this.$http
            .post(this.$http.defaults.analysisAPI + `api/busUnit/remove`, para)
            .then(response => {
              if (response.data.success === true) {
                this.getUnitAnalysisList(this.unitname)
                this.unitAnalysisList = ''
                this.unitAnalysisId = ''
                this.timestamp = new Date().getTime()
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
              } else {
                this.$message({
                  message: response.data.message,
                  type: 'error'
                })
              }
              this.delloading = false
            })
            .catch(error => {
              this.$message({
                message: '删除失败:' + error,
                type: 'error'
              })
              this.delloading = false
            })
        })
      } else {
        this.$message({
          message: '请选择要删除的年度',
          type: 'error'
        })
      }
    }
  },
  mounted () {
    this.getAnalysisRoles()
  }
}
</script>
<style>
#zhfxyp .el-table td, .el-table th{
  padding:2px 0;
}
#zhfxyp .el-textarea__inner{
  font-size:var(--size18) !important;
  color:#000 !important;
}
#zhfxyp h1{
  padding: 5px;
  border-bottom:1px solid #9CA3B3;
}
</style>
